package org.example.JUC.AQS;

import java.util.ArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @description:
 * @author: farprt
 * @create: 2022-09-05 22:33
 **/
public class ReentrantLockList {
    private ArrayList<String> array=new ArrayList<>();
    private final ReentrantReadWriteLock lock=new ReentrantReadWriteLock();
    private final Lock readLock=lock.readLock();
    private final Lock writeLock=lock.writeLock();

    public void add(String str){
        writeLock.lock();
        try {
            array.add(str);
        }finally {
            writeLock.unlock();
        }
    }

    public void remove(String str){
        writeLock.lock();
        try {
            array.remove(str);
        }finally {
            writeLock.unlock();
        }
    }

    public String get(int idx){
        readLock.lock();
        try {
            return array.get(idx);
        }finally {
            readLock.unlock();
        }
    }
}